package com.wangsh.usersCenter.util;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Callable;

import com.alibaba.fastjson.JSONObject;
import com.wangsh.usersCenter.common.util.ConstatFinalUtil;
import com.wangsh.usersCenter.common.util.SpringEmailUtil;
import com.wangsh.usersCenter.system.pojo.ARegion;
import com.wangsh.usersCenter.system.pojo.ASysLog;
import com.wangsh.usersCenter.system.service.ISystemDbService;
import com.wangsh.usersCenter.system.service.ISystemOperService;
import com.wangsh.usersCenter.users.pojo.AAdmins;

/**
 * 多线程
 * 
 * @author wangsh_sxt
 *
 */
public class ServiceCallableUtil implements Callable<Object>
{
	/* 发送邮件的标识 */
	public static final String OPERTYPE_SEND_EMAIL = "sendEmail";
	public static final String OPERTYPE_ADD_LOG = "addLog";
	/* 批量抓取地区 */
	public static final String OPERTYPE_BATCH_REGION = "batchRegion";
	
	/* 操作类型 */
	private String operType;
	/* 存储参数的map */
	private Map<String, Object> paramsMap;

	@Override
	public Object call() throws Exception
	{
		try
		{
			String thid = UUID.randomUUID().toString().replaceAll("-", "");
			Date createTime = new Date();
			paramsMap.put("createTime", createTime);
			paramsMap.put("thid", thid);
			// 1是开始,0是停止
			paramsMap.put("status", "1");
			ConstatFinalUtil.THREAD_MAP.put(thid, paramsMap);

			ConstatFinalUtil.SYS_LOGGER.info(thid + "-----线程池开始-----");
			if (OPERTYPE_SEND_EMAIL.equalsIgnoreCase(operType))
			{
				// 多线程发邮件
				sendEmail();
			} else if (OPERTYPE_ADD_LOG.equalsIgnoreCase(operType))
			{
				addLog();
			} else if(this.OPERTYPE_BATCH_REGION.equalsIgnoreCase(operType))
			{
				/* 批量抓取国家 */
				this.operBatchRegion();
			}

			// 将线程移除掉
			ConstatFinalUtil.THREAD_MAP.remove(thid);
			ConstatFinalUtil.SYS_LOGGER.info(thid + "-----线程池结束-----");
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("启动多线程报错了;条件:{}", paramsMap + ";operType:" + this.operType, e);
		}
		return 0;
	}

	/**
	 * 批量抓取国家
	 */
	private void operBatchRegion()
	{
		/* 获取一些重要的对象 */
		ISystemOperService systemOperService = (ISystemOperService) paramsMap.get("systemOperService");
		String requestUrl = paramsMap.get("requestUrl") + "" ;
		JSONObject regionJSON = (JSONObject) paramsMap.get("regionJSON");
		ARegion region = (ARegion) paramsMap.get("region") ;
		Map<String, Object> dataMapJava = (Map<String, Object>) paramsMap.get("dataMapJava") ;
		
		/* 存储其它城市的数据 */
		systemOperService.operBatchRegionChildrenUtil(requestUrl, regionJSON, 1, region,dataMapJava);
	}

	/**
	 * 采用多线程发邮件
	 */
	private void sendEmail()
	{
		SpringEmailUtil springEmailUtil = (SpringEmailUtil) paramsMap.get("springEmailUtil");
		String email = paramsMap.get("email") + "";
		String subject = paramsMap.get("subject") + "";
		String content = paramsMap.get("content") + "";
		springEmailUtil.sendHTMLMail(email, subject, content);
		ConstatFinalUtil.SYS_LOGGER.info(email + "-----发送邮件线程-----" + subject);
	}

	/**
	 * 添加一条日志
	 */
	private void addLog()
	{
		String ip = (String) paramsMap.get("ip");
		AAdmins admins = (AAdmins) paramsMap.get("admins");
		String content = (String) paramsMap.get("content");
		ISystemDbService systemDbService = (ISystemDbService) paramsMap.get("service");
		
		Map<String, String> paramsMap = new HashMap<String, String>();
		paramsMap.put("ip", ip);
		
		// TODO 添加一条日志
		ASysLog sysLog = new ASysLog();
		//sysLog.setAdminsId(admins.getId());
		sysLog.setName(admins.getName());
		sysLog.setContent(content);
		sysLog.setStatus(Byte.valueOf("1"));
		sysLog.setIp(ip);
		sysLog.setPubTime(new Date());
		sysLog.setCreateTime(new Date());
		sysLog.setUpdateTime(new Date());
		systemDbService.saveOneLogService(sysLog);
	}

	public void setOperType(String operType)
	{
		this.operType = operType;
	}

	public void setParamsMap(Map<String, Object> paramsMap)
	{
		this.paramsMap = paramsMap;
	}
}
